package com.example.wlzz.service.serviceImpl;

import com.example.wlzz.api.SmsApi;
import com.example.wlzz.entity.SmsRecord;
import com.example.wlzz.repository.SmsRepository;
import com.example.wlzz.service.SmsService;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class SmsServiceImpl implements SmsService{
    @Autowired
    private SmsRepository smsRepository;
    @Override
    public String sendSms(String phone,String phoneCode) {
         try {
                SmsSingleSender sender = new SmsSingleSender(SmsApi.APP_ID, SmsApi.APP_KEY);
                ArrayList<String> params = new ArrayList<String>();
                params.add(phoneCode);
                params.add("5");
                SmsSingleSenderResult result = sender.sendWithParam("86", phone, SmsApi.TEMP_ID, params, "", "", "");
                SmsRecord smsRecord = new SmsRecord();
                smsRecord.setCode(phoneCode);
                smsRecord.setCreateTime(new Date());
                smsRecord.setPhone(phone);
                smsRecord.setSmsIsOk(0);
                smsRepository.save(smsRecord);
                return result.toString();
            }catch (Exception e){
                return "fail";
            }
        }
}
